/* 
 * File:   TSFile.inl
 * Author: thunderliu
 *
 * Created on 2013年6月4日, 下午5:00
 */

#ifndef __TSFILE_INL__
#define	__TSFILE_INL__

#include "TSFile.h"



// CFileMapping

inline CFileMapping::CFileMapping()
: m_pAddr(MAP_FAILED)
, m_uLen(0)
{
}

inline CFileMapping::operator const void*() const
{
    return m_pAddr;
}

inline CFileMapping::operator void*()
{
    return m_pAddr;
}

inline void* CFileMapping::GetAddress()
{
    return m_pAddr;
}

inline size_t CFileMapping::GetSize() const
{
    return m_uLen;
}


// CStdFile

inline CStdFile::CStdFile()
: m_pFile(NULL)
{
}

inline CStdFile::~CStdFile()
{
    if (m_pFile)
    {
        Close(); // 父类Close;
    }
}

inline bool CStdFile::Open(const char* pFileName, const char* pMode)
{
    if (m_pFile)
    {
        Close();
    }
    m_pFile = fopen(pFileName, pMode);
    return (bool)m_pFile;
}

inline bool CStdFile::Eof()
{
    return feof(m_pFile);
}

template <typename TYPE>
inline size_t CStdFile::Write(const TYPE* pData, size_t uCount)
{
    return fwrite(pData, sizeof(TYPE), uCount, m_pFile);
}

template <typename TYPE>
inline size_t CStdFile::Read(TYPE* pData, size_t uCount)
{
    return fread(pData, sizeof(TYPE), uCount, m_pFile);
}

inline void CStdFile::Seek(long lOffset, FILE_ORIGIN eOrigin)
{
    fseek(m_pFile, lOffset, eOrigin);
}

inline size_t CStdFile::Tell()
{
    return ftell(m_pFile);
}

inline void CStdFile::Flush()
{
    fflush(m_pFile);
}

inline void CStdFile::Close()
{
    fclose(m_pFile);
    m_pFile = NULL;
}

inline bool CStdFile::IsOpened() const
{
    return (bool)m_pFile;
}

inline FILE* CStdFile::GetHandler()
{
    return m_pFile;
}

inline CStdFile::operator FILE*()
{
    return GetHandler();
}


// CAsyncWriteFile

inline bool CAsyncWriteFile::OnWork(CBlock16* pBlock)
{
    fwrite(pBlock->GetBuffer(), 1, pBlock->GetSize(), m_pFile->GetHandler());
    return true;
}

inline bool CAsyncWriteFile::OnEmpty()
{
    m_pFile->Flush();
    return true;
}

inline bool CAsyncWriteFile::OnStop()
{
    m_pFile->Flush();
    return true;
}


// CAsyncFile

inline CAsyncFile::~CAsyncFile()
{
    if (m_pFile)
    {
        Close();
    }
}

inline bool CAsyncFile::Open(const char* pFileName, const char* pMode)
{
    if (!CStdFile::Open(pFileName, pMode))
    {
        return false;
    }
    m_oAsyncWriteFile.Init(CONST_MAX_BLOCK);
    m_oAsyncWriteFile.Start();
    
    return true;
}

template <typename TYPE>
inline size_t CAsyncFile::AsyncWrite(const TYPE* pData, size_t uCount)
{
    CBlock16* pBlock = CBlock16::CreateBlock(sizeof(TYPE) * uCount);
    memcpy(pBlock->GetBuffer(), pData, pBlock->GetSize());
    m_oAsyncWriteFile.AddBlock(pBlock);
    return pBlock->GetSize();
}

inline void CAsyncFile::Close()
{
    m_oAsyncWriteFile.Stop();
    m_oAsyncWriteFile.Wait();
    CStdFile::Close();
}


#endif	/* __TSFILE_INL__ */

